package com.mushuilingfeng.ddd.user.domain.user.entity;

import cn.hutool.core.util.StrUtil;
import com.mushuilingfeng.ddd.framework.factory.DomainRepositoryFactory;
import com.mushuilingfeng.ddd.user.domain.base.BaseEntity;
import com.mushuilingfeng.ddd.user.domain.base.BusinessException;
import com.mushuilingfeng.ddd.user.domain.base.DomainArgumentException;
import com.mushuilingfeng.ddd.user.domain.base.Ii8nCode;
import com.mushuilingfeng.ddd.user.domain.user.repository.IUserRepository;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author 沐水淋风
 * @date 2024/10/2
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class UserLoginAccount extends BaseEntity {
    private String loginAccount;
    private String loginPassword;
    
    @Override
    public void simpleCheckParams() {
        if (StrUtil.isBlankIfStr(loginAccount)) {
            throw new DomainArgumentException(Ii8nCode.LOGIN_ACCOUNT_CANNOT_BE_EMPTY);
        }
        if (StrUtil.isBlankIfStr(loginPassword)) {
            throw new DomainArgumentException(Ii8nCode.LOGIN_PASSWORD_CANNOT_BE_EMPTY);
        }
        // 检查登陆密码是否强大
        if (loginPassword.length() < 6) {
            throw new BusinessException(Ii8nCode.PASSWORD_TOO_SIMPLE);
        }
    }
    
    public void updatePassword(String newPassword) {
        if (this.loginPassword.equals(newPassword)) {
            throw new BusinessException(-1, "新旧密码一致！");
        }
        if (newPassword.length() < 6) {
            throw new BusinessException(Ii8nCode.PASSWORD_TOO_SIMPLE);
        }
        DomainRepositoryFactory.getRepository(IUserRepository.class).updatePassword(this.id, newPassword);
    }
    
    public String getLoginPassword() {
        return loginPassword;
    }
}
